package com.weds.edjk.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
/**
 * @Author zcz
 * @Description 日期工具类
 * @Date 2020-6-9
 */
public class DateUtils {
    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 日期转字符串
     * @param date
     * @param format
     * @return
     */
    public static String getStr(Date date,String format){
        SimpleDateFormat formatter=new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 当前日期
     * @return
     */
    public static String getNowDate(){
        Date date = new Date();
        SimpleDateFormat formatter=new SimpleDateFormat(YYYY_MM_DD);
        return formatter.format(date);
    }

    /**
     * 字符串转日期
     * @param strDate
     * @return
     * @throws ParseException
     */
    public static  Date parse(String strDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        return sdf.parse(strDate);
    }

    /**
     * 工龄
     * @param workTime
     * @return
     */
    public static int getWorkAge(Date workTime){
        if(Objects.isNull(workTime)){
            return 0;
        }
        Date nowTime = new Date();
        int year = 0;
        //当前时间的年月日
        Calendar cal = Calendar.getInstance();
        cal.setTime(nowTime);
        int nowYear = cal.get(Calendar.YEAR);
        int nowMonth = cal.get(Calendar.MONTH);
        int nowDay = cal.get(Calendar.DAY_OF_MONTH);
        //开始工作时间的年月日
        cal.setTime(workTime);
        int workYear = cal.get(Calendar.YEAR);
        int workMonth = cal.get(Calendar.MONTH);
        int workDay = cal.get(Calendar.DAY_OF_MONTH);
        //得到工龄
        year = nowYear - workYear; //得到年差
        //若目前月数少于开始工作时间的月数，年差-1
        if (nowMonth < workMonth){
            year  = year - 1;
        }else if (nowMonth == workMonth){
            //当月数相等时，判断日数，若当月的日数小于开始工作时间的日数，年差-1
            if (nowDay < workDay){
                year = year - 1;
            }
        }

        return year;
    }

    /**
     * 计算年龄
     * @param birthDay
     * @return
     * @throws Exception
     */
    public static int getAge(Date birthDay){
        if(Objects.isNull(birthDay)){
            return 0;
        }
        Calendar cal = Calendar.getInstance();
        //出生日期晚于当前时间，无法计算
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        //当前年份
        int yearNow = cal.get(Calendar.YEAR);
        //当前月份
        int monthNow = cal.get(Calendar.MONTH);
        //当前日期
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        //计算整岁数
        int age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                //当前日期在生日之前，年龄减一
                if (dayOfMonthNow < dayOfMonthBirth){
                    age--;
                }
            }else{
                //当前月份在生日之前，年龄减一
                age--;
            }
        }
        return age;
    }

}
